import 'package:animations/animations.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:series_play/comm/base/base_state.dart';
import 'package:series_play/pages/search/controller/search_controller.dart'
    as search;
import 'package:series_play/pages/search/search_default_page.dart';
import 'package:series_play/pages/search/search_result_page.dart';

import '../../generated/assets.dart';
import '../../widget/app_color.dart';
import '../../widget/app_image.dart';

class SearchPage extends StatefulWidget {
  const SearchPage({super.key});

  @override
  State<SearchPage> createState() => _SearchPageState();
}

class _SearchPageState
    extends BaseControllerState<SearchPage, search.SearchController> {
  /// 延迟等待controller初始化后再设置
  late Widget defaultPage;
  late Widget searchResultPage;

  TextEditingController _textEditingController = TextEditingController();

  @override
  void initState() {
    super.initState();
    defaultPage = SearchDefaultPage(mController);
    searchResultPage = SearchResultPage(mController);
  }

  @override
  Widget buildPage(BuildContext context) {
    return PageTransitionSwitcher(
      transitionBuilder:
          (
            Widget child,
            Animation<double> animation,
            Animation<double> secondaryAnimation,
          ) {
            return FadeThroughTransition(
              animation: animation,
              secondaryAnimation: secondaryAnimation,
              fillColor: Colors.transparent,
              child: child,
            );
          },
      child: _buildPage(),
    );
  }

  @override
  bool safeAreaFormBottom() => false;

  Widget _buildPage() {
    return Obx(() {
      return mController.pageType.value == 0 ? defaultPage : searchResultPage;
    });
  }

  @override
  search.SearchController createController() {
    return search.SearchController();
  }

  @override
  Widget? getTitleWidget() {
    // 这里高度不适配 固定
    return Container(
      height: 36.w,
      decoration: BoxDecoration(
        color: AppColor.white_20,
        borderRadius: BorderRadius.circular(4).r,
      ),
      alignment: Alignment.centerLeft,
      child: Stack(
        children: [
          TextField(
            controller: _textEditingController,
            maxLines: 1,
            keyboardType: TextInputType.text,
            textAlignVertical: TextAlignVertical.center,
            style: TextStyle(fontSize: 14.sp, color: AppColor.white_60),
            decoration: InputDecoration(
              isCollapsed: true,
              hintStyle: TextStyle(fontSize: 14.sp, color: AppColor.white_60),
              hintText: '新手专属任务',
              border: InputBorder.none,
              contentPadding: const EdgeInsets.symmetric(horizontal: 36).w,
            ),
            onSubmitted: (value) {
              mController.search(value);
            },
            onChanged: (value) {
              mController.searchKey.value = value;
            },
          ),
          Positioned(
            left: 12.w,
            top: 2.w,
            bottom: 0,
            child: AppImage(Assets.imagesIcSearch, size: 18),
          ),
          Positioned(
            right: 12.w,
            top: 0,
            bottom: 0,
            child: Obx(() {
              return Visibility(
                visible: mController.searchKey.isNotEmpty,

                child: AppImage(
                  Assets.imagesIcInputClear,
                  size: 18,
                  onTap: () {
                    _textEditingController.clear();
                    mController.clearSearch();
                  },
                ),
              );
            }),
          ),
        ],
      ),
    );
  }

  @override
  double? titleSpacing() {
    return 0;
  }

  @override
  List<Widget>? actions() {
    return [SizedBox(width: 16)];
  }

  @override
  bool showDefaultBackground() {
    return true;
  }
}
